<?php
/**
 * 
 * 根据话题抓取微博数据
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


ini_set("display_errors",false);


define("CRON_NAME","update_user_head");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
//$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());


include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");

class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		$start_id = intval(intval($this->start_id));
		$sql = "select count(*) as count from qq_user_info where  uid > $start_id";
		
		$db_base = new db_base();
		$count = $db_base->select($sql);
		$count = $count[0]["count"];
		echo "tag_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		
	}	
	
	function get_list_by_page($p)
	{
		$start = ($p-1)*PCOUNT;
		$start_id = intval(intval($this->start_id));
		$sql = "select openid,uid from qq_user_info where uid > $start_id order by uid limit  $start,".PCOUNT;
		$mblog_from_api = new mblog_from_api();
		$db_base = new db_base();
		$list = $db_base->select($sql);
		
		foreach($list as $value)
		{
			$openid = $value["openid"];
			$uid = $value["uid"];
			if(!$openid)
			{
				continue;
			}
			else
			{
			
				
				$sql ="select head_url,create_nickname from mblog_pic_content where create_uid={$uid} limit 1";
				$get_one = $db_base->select($sql);
				if(!$get_one)
				{
					$sql ="select head_url,create_nickname from mblog_want_content where create_uid={$uid} limit 1";
					$get_one = $db_base->select($sql);
				
				}
				
				if(!$get_one)
				{
					$sql ="select head_url,create_nickname from mblog_video_content where create_uid={$uid} limit 1";
					$get_one = $db_base->select($sql);
				
				}
				
				if(!$get_one)
				{
					$sql ="select head_url,create_nickname from mblog_meinv_content where create_uid={$uid} limit 1";
					$get_one = $db_base->select($sql);
				
				}
				/*if(is_numeric($openid))
				{
					$mblog_info = $mblog_from_api->api_get_user_info_by_uid($openid);
					$mblog_info = $mblog_info["data"]["result"];
					
					$user_nickname = $mblog_info["name"];
					$head_url = $mblog_info["profile_image_url"];
					if($mblog_info["gender"] == "f")
					{
						$sex=2;
					}
					elseif($mblog_info["gender"] == "m")
					{
						$sex=1;
					}
					else
					{
						$sex=0;
					}
				}
				else
				{
				
					$mblog_get_mblog_from_qq = new mblog_get_mblog_from_qq();
					$user_info = $mblog_get_mblog_from_qq->get_user_info_by_uid($openid);
					
					$user_nickname =  $user_info["data"]["nick"];
					$sex =  $user_info["data"]["sex"];
					$head_url = $user_info["data"]["head"]."/50/";
				}
				*/
				
				
				if($get_one)
				{
					$user_nickname = $get_one[0]["create_nickname"];
					$head_url = $get_one[0]["head_url"];
					
					if($user_nickname && $head_url)$sql ="update qq_user_info set user_nickname='{$user_nickname}' ,head_url='{$head_url}' where uid = $uid";
					$db_base->query($sql);
				}
				else
				{
				
					//$sql = 'update qq_user_info set status = 0 where uid = '.$uid;
					$db_base->query($sql);
				}
				
				
			}
			
			$this->replace_into_data($uid);
			
			
			
		}
		
	}
	
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
